using System;
using System.Collections.Generic;
using System.Text;
using GarageGames.Torque.GUI;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Xyne.Regions;
using GarageGames.Torque.T2D;
using GarageGames.Torque.Sim;
using Microsoft.Xna.Framework.Input;
using GarageGames.Torque.GameUtil;
using GarageGames.Torque.Core;
using GarageGames.Torque.Platform;
using GarageGames.Torque.GFX;
using System.Collections;
using Xyne.Utilities;
using Xyne.Templates.PuzzlePieceTemplate;
using Xyne.Events;
using GarageGames.Torque.Core.Xml;
using System.Threading;
using Xyne.Templates.PuzzleGlowTemplate;
using Xyne.Templates;

namespace Xyne.GUI
{
    class MainGameGUI : GUISceneview, IGUIScreen
    {
        bool shitLoaded;

        // Regions
        private PuzzleRegion puzzleRegion;
        private HandRegion handRegion;
        private BattleRegion battleRegion;
        private QueueRegion queueRegion;
        
        private PlayerController playerController;

        // Font Test
        private ArrayList texts;
        private Vector2 textPositionModifier;

        // Semaphore

        public Vector2 TextPositionModifier
        {
            get
            {
                return textPositionModifier;
            }
        }

        private PauseMenuGUI pauseControl;
        private EndGameGUI endGameControl;

        #region Constructor
        public MainGameGUI()
        {

            shitLoaded = false;
            GUIStyle stylePlayGUI = new GUIStyle();

            Name = "MainGame";
            Style = stylePlayGUI;
            Size = Game.Instance.ClientBounds;

            pauseControl = new PauseMenuGUI();

            pauseControl.Folder = this;

            endGameControl = new EndGameGUI();
            endGameControl.Folder = this;

            EventManager.Instance.Callbacks.Add(new EventManager.EventReceivedDelegate(ReceiveEventManager));

            OnGUIWake = new OnWakeDelegate(StartMainGUI);
            
            
        }
        #endregion

        #region Public Methods

        public void ReceiveEventManager(Event myEvent)
        {
            // We can handle these cases differently later...
            if (myEvent is GameQuitEvent)
            {
                if (Game.Network != null)
                    Game.Network.RemoveStateEntity(battleRegion);

                endGameControl.EnableMenu();
            }
            else if (myEvent is GamePauseEvent)
            {
                if (((GamePauseEvent)myEvent).Pause && !pauseControl.Enabled)
                {
                    pauseControl.Origin = ((GamePauseEvent)myEvent).Gamer;
       /*             if (pauseControl.Origin == Game.Network.Gamer.Gamertag)
                    {
                        pauseControl.MenuText = "You paused the game! PAUSE MENU PUSH START OR BACK OR (SPACE) TO UNPAUSE, PRESS Y OR (ESC) to exit the game";
                    }
                    else
                    {
                        pauseControl.MenuText = pauseControl.Origin + " paused the game! PAUSE MENU PRESS Y OR (ESC) to exit the game";
                    }*/
                    
                    pauseControl.EnablePauseScreen();
                }
                else if (!((GamePauseEvent)myEvent).Pause && pauseControl.Enabled)
                {
                    pauseControl.DisablePauseScreen();
                }
            }
            else if (myEvent is GameLostEvent)
            {
                if (Game.Network != null)
                    Game.Network.RemoveStateEntity(battleRegion);

                endGameControl.EndGameText = "You Lost - Press 'Esc' or 'Start' to go back to the main menu";
                endGameControl.EnableMenu();
            }
            else if (myEvent is GameWonEvent)
            {
                if (Game.Network != null)
                    Game.Network.RemoveStateEntity(battleRegion);

                endGameControl.EndGameText = "You Won! - Press 'Esc' or 'Start' to go back to the main menu";
                endGameControl.EnableMenu();
            }
        }

        public void StartMainGUI(GUIControl control)
        {
            Game.Instance.Window.Title = "Xyne";
            pauseControl.Init();
            endGameControl.Init();

            #region overlay

            GUIBitmap backgroundBitmap = new GUIBitmap();
            GUIBitmapStyle bitmapStyle = new GUIBitmapStyle();
            bitmapStyle.IsOpaque = false;
            //bitmapStyle.FillColor[CustomColor.ColorBase] = new Microsoft.Xna.Framework.Graphics.Color(0, 0, 0, 128);
            backgroundBitmap.Bitmap = @"data\images\ui\overlay\widescreen.png";
            backgroundBitmap.Style = bitmapStyle;
            backgroundBitmap.Position = new Vector2(0, 0);
            backgroundBitmap.Size = Size;

            backgroundBitmap.Visible = true;

            backgroundBitmap.Folder = this;
            #endregion
        }
        #endregion

      //  private void InitializeUI(string sceneName, TorqueSceneData sceneData)
        public void InitializeUI()
        {
            bool isWideScreen = GFXDevice.IsWideScreen((int)Game.Instance.ClientBounds.X, (int)Game.Instance.ClientBounds.Y);

            Rectangle puzzleRegionRectangle = Rectangle.Empty;
            Rectangle handRegionRectangle = Rectangle.Empty;
            Rectangle queueRegionRectangle = Rectangle.Empty;
            Rectangle battleRegionRectangle = Rectangle.Empty;

            T2DSceneCamera camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>();

            if (isWideScreen)
            {
                camera.Extent = new Vector2(1024, 576);
                camera.CenterPosition = new Vector2(512, 288);

                battleRegionRectangle = new Rectangle(0, 0, 1024, 130);
                puzzleRegionRectangle = new Rectangle(10, 140, 650, 420);
                queueRegionRectangle = new Rectangle(670, 140, 120, 420);
                handRegionRectangle = new Rectangle(800, 140, 200, 420);
                textPositionModifier = new Vector2(1199f / 1024f, 671f / 576f);

            }
            else
            {
                battleRegionRectangle = new Rectangle(0, 0, 1024, 170);
                puzzleRegionRectangle = new Rectangle(10, 280, 736, 476);
                queueRegionRectangle = new Rectangle(10, 175, 735, 96);
                handRegionRectangle = new Rectangle(755, 175, 265, 580);
                textPositionModifier = new Vector2(1, 1);
            }

            battleRegion = new BattleRegionImpl(battleRegionRectangle);
            queueRegion = new QueueRegionImpl(queueRegionRectangle);
            puzzleRegion = new PuzzleRegionImpl(puzzleRegionRectangle, queueRegion);
            handRegion = new HandRegionImpl(handRegionRectangle, puzzleRegion);
            playerController = new PlayerController(handRegion);
            playerController.Init();
            handRegion.Init(queueRegion);
            queueRegion.Init(puzzleRegion);

            puzzleRegion.Start();
            AnimationManager.Instance.Init();

            if (Game.Network != null)
            {
                Game.Network.AddStateEntity(battleRegion);
                Game.Network.AddStateEntity(queueRegion);
            }
            shitLoaded = true;

        }

        public override void OnUnloaded()
        {

            puzzleRegion = null;
            handRegion = null;
            battleRegion = null;
            queueRegion = null;
            playerController = null;

            TorqueObjectDatabase.Instance.Unregister(TorqueObjectDatabase.Instance.RootFolder);
            base.OnUnloaded();

        }

        private void OnExit(float val)
        {
            if (val > 0)
                Game.Instance.Exit();
        }

        #region Text Management
        public ArrayList GameTexts
        {
            get
            {
                if (texts == null)
                {
                    texts = new ArrayList();

                }
                return texts;
            }

            set
            {
                texts = value;
            }
        }
        #endregion        

        public override void OnPreRender()
        {

            base.OnPreRender();
        }

        public override void OnRender(Vector2 offset, GarageGames.Torque.MathUtil.RectangleF updateRect)
        {

     //       while (!shitLoaded)
     //       {
    //            Thread.Sleep(200);
    //        }
            base.OnRender(offset, updateRect);
            handRegion.RefreshCards();
        }
    }

    
}
